<template>
  <div>
    <teleport to="body">
      <!-- 蒙版 -->
      <transition name="fade">
        <div
          v-if="isVisiable"
          @click="isVisiable = false"
          class="w-screen h-screen bg-zinc-900/80 z-40 fixed top-0 left-0"
        ></div>
      </transition>
      <!-- 内容 -->
      <transition name="popup-up-down">
        <div
          v-if="isVisiable"
          v-bind="$attrs"
          class="w-screen bg-white dark:bg-zinc-800 z-50 fixed bottom-0 rounded-t"
        >
          <slot />
        </div>
      </transition>
    </teleport>
  </div>
</template>

<script setup>
import { watch } from '@vue/runtime-core'
import { useScrollLock, useVModel } from '@vueuse/core'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  }
})
// 不需要这个方法
// defineEmits(['update:modelValue'])

// 是一个响应式数据，当isVisiable值发生改变时会自动触发emit修改modelValue
const isVisiable = useVModel(props)

// 蒙版出现锁定滚动
const lockStatus = useScrollLock(document.body)

watch(
  isVisiable,
  (val) => {
    lockStatus.value = val
  },
  {
    immediate: true
  }
)
</script>
<style lang="scss" scoped>
.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
.popup-up-down-enter-active,
.popup-up-down-leave-active {
  transition: all 0.3s;
}
.popup-up-down-leave-to,
.popup-up-down-enter-from {
  transform: translateY(100%);
}
</style>
